﻿using System;
using System.Collections.Generic;
using System.Linq;


namespace ProductInfo.Core.Data.Repository
{
	public class ReleaseNoteEntry : ProductInfo.Core.Data.Repository.IReleaseNoteEntry
	{
		private readonly ProductInfo.Core.Data.Context.ProductInfoDataContext c_productInfoDataContext;


		public ReleaseNoteEntry()
		{
			this.c_productInfoDataContext = new ProductInfo.Core.Data.Context.ProductInfoDataContext();
		}


		public IEnumerable<ProductInfo.Core.Models.ReleaseNoteEntry> FetchAll()
		{
			return this.Map(this.c_productInfoDataContext.ReleaseNoteEntries);
		}


		public ProductInfo.Core.Models.ReleaseNoteEntry FetchByKey(
			int key)
		{
			var _result = (
				from _releaseNoteEntry in this.c_productInfoDataContext.ReleaseNoteEntries
				where _releaseNoteEntry.Id == key
				select new ProductInfo.Core.Models.ReleaseNoteEntry(
					_releaseNoteEntry.Id,
					_releaseNoteEntry.ProductId,
					_releaseNoteEntry.Version,
					_releaseNoteEntry.Detail,
					_releaseNoteEntry.Author,
					_releaseNoteEntry.EditTimestamp)).First();

			return _result;
		}


		public void Update(
			ProductInfo.Core.Models.ReleaseNoteEntry subject)
		{
			var _releaseNoteEntry = this.c_productInfoDataContext.ReleaseNoteEntries.Single(_releaseNote => _releaseNote.Id == subject.Id);
			_releaseNoteEntry.ProductId = subject.ProductId;
			_releaseNoteEntry.Version = subject.Version;
			_releaseNoteEntry.Detail = subject.Detail;
			_releaseNoteEntry.Author = subject.Author;
			_releaseNoteEntry.EditTimestamp = DateTime.UtcNow;

			this.c_productInfoDataContext.SubmitChanges();
		}


		public void Update(
			string originalValue,
			string updateValue)
		{
			var _releaseNoteEntries = (
				from _releaseNoteEntry in this.c_productInfoDataContext.ReleaseNoteEntries
				where _releaseNoteEntry.Version == originalValue
				select _releaseNoteEntry);

			foreach (var _releaseNoteEntry in _releaseNoteEntries) { _releaseNoteEntry.Version = updateValue; }

			this.c_productInfoDataContext.SubmitChanges();
		}


		public void Create(
			ProductInfo.Core.Models.ReleaseNoteEntry subject)
		{
			this.c_productInfoDataContext.ReleaseNoteEntries.InsertOnSubmit(this.Map(subject));
			
			this.c_productInfoDataContext.SubmitChanges();
		}


		public IEnumerable<ProductInfo.Core.Models.ReleaseNoteEntry> Map(
			IEnumerable<ProductInfo.Core.Data.Context.ReleaseNoteEntry> source)
		{
			return
				from _releaseNoteEntry in source
				orderby _releaseNoteEntry.ProductId, _releaseNoteEntry.EditTimestamp descending
				select new ProductInfo.Core.Models.ReleaseNoteEntry(
					_releaseNoteEntry.Id,
					_releaseNoteEntry.ProductId,
					_releaseNoteEntry.Version,
					_releaseNoteEntry.Detail,
					_releaseNoteEntry.Author,
					_releaseNoteEntry.EditTimestamp);
		}


		public ProductInfo.Core.Data.Context.ReleaseNoteEntry Map(
			ProductInfo.Core.Models.ReleaseNoteEntry source)
		{
			var _result = new ProductInfo.Core.Data.Context.ReleaseNoteEntry();
			_result.ProductId = source.ProductId;
			_result.Version = source.Version;
			_result.Detail = source.Detail;
			_result.Author = source.Author;
			_result.EditTimestamp = DateTime.UtcNow;

			return _result;
		}
	}
}